package service.impl;

import java.util.List;
import java.util.Map;

import dao.impl.StudentDaoImpl;
import entity.PageBean;
import entity.Student;
import dao.IStudentDao;
import service.IStudentService;
//StudentServiceImpl 写的是业务逻辑，在增删改查之上要实现的功能。
public class StudentServiceImpl implements IStudentService {
	private IStudentDao studentDao = new StudentDaoImpl();
	@Override
	public boolean insert(Student student) {
		List<Student> list =  studentDao.search(student.getStudent_name());
		if (list.size() > 0) {
			System.out.println("该用户已经存在");
			// 该名字用户已经存在
			return false;
		} else {
			int count = studentDao.insert(student);
			return count == 1 ? true : false;
		}

	}@Override
	public List<Student> selectAll() {
		List<Student> list = studentDao.selectAll();
		return list;
	}@Override
	public boolean update(Student student) {
		int count = studentDao.update(student);
		return count == 1 ? true : false;
	}
	@Override
	public List<Student> search(String searchName) {
		return studentDao.search(searchName);
	}
	@Override
	public boolean deleteById(Integer id) {
		int count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}
	@Override
	public Student selectById(int id) {
		return studentDao.selectById(id);
	}
	@Override
	public PageBean pageList(Integer pageNo, Integer pageSize) {
		PageBean pageBean = new PageBean();
		pageBean.setPageNo(pageNo);
		pageBean.setPageSize(pageSize);
		int count = studentDao.getTotalCount();
		int totalPage = (int) Math.ceil((double)count / pageSize);
		pageBean.setTotalPage(totalPage);

		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentDao.pageList(offset, pageSize);
		pageBean.setList(list);
		return pageBean;
	}
	@Override
	public boolean checkName(String name) {
		List<Student> list = studentDao.selectByName(name);
		return list.size() > 0 ? true : false;
	}
	@Override
	public List<Map<String, Object>> selectStudentBanjiInfo() {
		
		return studentDao.selectStudentBanjiInfo();
	}



}
